<?php

class RoomController extends Controller {

	/**
	 * @var string the default layout for the views.
	 * Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout = '//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow',
				'actions'=>array('index', 'view'),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array('create', 'update'),
				'users'=>array('@'),
			),
			array('allow',
				'actions'=>array('admin', 'delete'),
				'users'=>array('admin'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id) {
		$model = $this->loadModel($id);
		/*
		  $config['criteria'] = array('condition'=>'roomId = ' . $model->_id);
		  $lightDataProvider = new CActiveDataProvider('Light', $config);
		 */
		$this->menu = $this->createMenu('view', $model->_id);
		$this->render('view', array('model'=>$model));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view'
	 * page.
	 */
	public function actionCreate() {
		$model = new Room;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['Room'])) {
			$model->attributes = $_POST['Room'];
			if ($model->save()) {
				$this->redirect(array('view', 'id'=>$model->_id));
			}
		}

		$this->render('create', array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view'
	 * page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id) {
		$model = $this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['Room'])) {
			$model->attributes = $_POST['Room'];
			if ($model->save()) {
				$this->redirect(array('view', 'id'=>$model->_id));
			}
		}

		$this->render('update', array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index'
	 * page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id) {
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we
			// should not redirect the browser
			if (!isset($_GET['ajax'])) {
				if (isset($_POST['returnUrl'])) $url = $_POST['returnUrl'];
				else $url = array('admin');
				$this->redirect($url);
			}
		} else {
			$message = 'Invalid request. ';
			$message .= 'Please do not repeat this request again.';
			throw new CHttpException(400, $message);
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Room');
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin() {
		$model = new Room('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['Room'])) $model->attributes = $_GET['Room'];

		$this->render('admin', array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET
	 * variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id) {
		$model = Room::model()->findByPk((int) $id);
		if ($model === null) {
			throw new CHttpException(404, 'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model) {
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'room-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	public function createMenu($current='', $id=false, $contextOnly=false,
		$mode='text') {

		$menus = array();
		if (!$contextOnly) {
			if ($current != 'index') {
				$menus[] = array('label'=>'List Room', 'url'=>array('index'));
			}
			if ($current != 'create') {
				$menus[] = array(
					'label'=>'Create Room',
					'url'=>array('create')
				);
			}
		}
		if ($id !== false) {
			if ($mode == 'text') {
				$view = 'View Room';
				$update = 'Update Room';
				$delete = 'Delete Room';
				$listLight = 'List Light';
				$addLight = 'Add Light';
				$listHvac = 'List HVAC';
				$addHvac = 'Add HVAC';
				$listDoor = 'List Door';
				$addDoor = 'Add Door';
				$listMood = 'List Mood';
				$addMood = 'Add Mood';
			} else {
				$fugue = Yii::app()->request->baseUrl . '/images/fugue';
				$view = "<img src=\"$fugue/magnifier-zoom.png\" " .
					"title=\"View\" />";
				$update = "<img src=\"$fugue/pencil.png\" title=\"Update\" />";
				$delete = "<img src=\"$fugue/cross-script.png\" " .
					"title=\"Delete\" />";
				$listLight = "<img src=\"$fugue/light-bulb.png\" " .
					"title=\"List Light\" />";
				$addLight = "<img src=\"$fugue/light-bulb--plus.png\" " .
					"title=\"Add Light\" />";
				$listHvac = "<img src=\"$fugue/target.png\" " .
					"title=\"List HVAC\" />";
				$addHvac = "<img src=\"$fugue/target--plus.png\" " .
					"title=\"Add HVAC\" />";
				$listDoor = "<img src=\"$fugue/door.png\" " .
					"title=\"List Door\" />";
				$addDoor = "<img src=\"$fugue/door--plus.png\" " .
					"title=\"Add Door\" />";
				$listMood = "<img src=\"$fugue/heart.png\" " .
					"title=\"List Mood\" />";
				$addMood = "<img src=\"$fugue/heart--plus.png\" " .
					"title=\"Add Mood\" />";
			}

			if ($current != 'view') {
				$menus[] = array(
					'label'=>$view,
					'url'=>array('view', 'id'=>$id)
				);
			}
			if ($current != 'update') {
				$menus[] = array(
					'label'=>$update,
					'url'=>array('update', 'id'=>$id)
				);
			}
			$menus[] = array(
				'label'=>$delete,
				'url'=>'#',
				'linkOptions'=>array(
					'submit'=>array(
						'delete',
						'id'=>$id,
					),
					'confirm'=>'Are you sure you want to delete this item?',
				)
			);
			$menus[] = array(
				'label'=>$listLight,
				'url'=>array('light/index', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$addLight,
				'url'=>array('light/create', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$listHvac,
				'url'=>array('hvac/index', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$addHvac,
				'url'=>array('hvac/create', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$listDoor,
				'url'=>array('door/index', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$addDoor,
				'url'=>array('door/create', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$listMood,
				'url'=>array('mood/index', 'roomId'=>$id),
			);
			$menus[] = array(
				'label'=>$addMood,
				'url'=>array('mood/create', 'roomId'=>$id),
			);
		}
		if (!$contextOnly && $current != 'admin') {
			$menus[] = array('label'=>'Manage Room', 'url'=>array('admin'));
		}
		return $menus;
	}

}
